﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;
using System.IO;

namespace MusicManager
{
    public partial class MusicListForm : Form
    {
        private MainForm father = null;
        private int indexNum;
        //private List<string> itemSelected = new List<string>();//contains the paths selected

        public MusicListForm(MainForm father,string title, int indexNum)
        {
            InitializeComponent();
            this.father = father;
            this.indexNum = indexNum;
            if (title != null && title != "")
                this.Text = title;
            grid_list.AllowUserToDeleteRows = false;
            grid_list.AllowUserToAddRows = false;
            grid_list.AllowDrop = true;
            grid_list.KeyUp += OnKeyUp;
            grid_list.CellDoubleClick += gridCell_DoubleClick;
            grid_list.DragEnter += grid_DragEnter;
            grid_list.DragDrop += grid_DragDrop;
            //grid_list.CellMouseDown += grid_CellMouseDown;
            buttonDrag.MouseDown += buttonDrag_MouseDown;
            this.FormClosing += form_Closing;
        }

        public void InitList(IWMPPlaylist list)
        {
            if (list != null)
            {
                for (int i = 0; i < list.count; i++)
                {
                    IWMPMedia song = list.get_Item(i);
                    string name = song.name;
                    string duration = song.durationString;
                    string path = song.sourceURL;
                    grid_list.Rows.Add(new object[] { i + 1, name, duration, path });
                    grid_list.Rows[i].HeaderCell.Value = (i + 1) + "";
                }
            }
        }

        public void RefreshUi()
        {
            grid_list.Rows.Clear();
            InitList(father.GetCurList());
        }

        //private void OnCellClick(object sender, EventArgs e)
        //{
            //string curPath = grid_list.CurrentRow.Cells["path"].Value.ToString();
            //if (grid_list.CurrentRow.Selected)
            //{
            //    itemSelected.Add(curPath);
            //}
            //else
            //{
            //    itemSelected.Remove(curPath);
            //}
       // }

        private void OnKeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Delete:
                    foreach (DataGridViewRow row in grid_list.SelectedRows)
                    {
                        string path = row.Cells["path"].Value.ToString();
                        father.DeleteItem(indexNum, path);
                        grid_list.Rows.Remove(row);
                    }
                    //RefreshUi();
                    break;
            }
        }

        private void buttonSaveAs_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            dia.ShowDialog();
            string dir = dia.SelectedPath;
            try
            {
                foreach (DataGridViewRow row in grid_list.SelectedRows)
                {
                    string path = row.Cells["path"].Value.ToString();
                    string name = path.Substring(path.LastIndexOf(@"\"));
                    File.Copy(path, dir + @"\" + name);
                }
                //MessageBox.Show("复制完毕！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void AddFiles(string[] fileNames)
        {
            ////father.InitPlayerList(fileNames);
            //father.AddToPlayerList(fileNames);
            //RefreshUi();
            for (int i = 0; i < fileNames.Length; i++)
            {
                string path = fileNames[i];
                int sta = path.LastIndexOf(@"\")+1;
                int end = path.LastIndexOf(@".")-1;
                string name = path.Substring(sta, end-sta+1);
                string duration = "";
                grid_list.Rows.Add(new object[] { i + 1, name, duration, path });
                grid_list.Rows[i].HeaderCell.Value = (i + 1) + "";
            }
            father.AddToPlayList(indexNum, fileNames);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog dia = new OpenFileDialog();
            dia.Multiselect = true;
            if (dia.ShowDialog() == DialogResult.OK)
            {
                //String[] fileNames = dia.FileNames;
                ////father.InitPlayerList(fileNames);
                //father.AddToPlayerList(fileNames);
                //RefreshUi();
                AddFiles(dia.FileNames);
            }
        }

        private void buttonMove_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dia = new FolderBrowserDialog();
            dia.ShowDialog();
            string dir = dia.SelectedPath;
            try
            {
                foreach (DataGridViewRow row in grid_list.SelectedRows)
                {
                    string path = row.Cells["path"].Value.ToString();
                    string name = path.Substring(path.LastIndexOf(@"\"));
                    File.Move(path, dir + @"\" + name);
                }
                //MessageBox.Show("移动完毕！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void gridCell_DoubleClick(object sender, EventArgs e)
        {
            if (grid_list.CurrentRow != null)
            {
                List<string> paths = new List<string>();
                foreach (DataGridViewRow row in grid_list.Rows)
                {
                    paths.Add(row.Cells["path"].Value.ToString());
                }
                string curPath = grid_list.CurrentRow.Cells["path"].Value.ToString();
                if (father.ChangePlayList(indexNum, paths.ToArray()) == true)
                    RefreshUi();
                father.GetPlayer().Ctlcontrols.playItem(father.GetMedia(curPath));
            }
        }

        private void grid_DragDrop(object sender, DragEventArgs e)
        {
            string[] fileNames = null;
            if (e.Data.GetDataPresent(typeof(String[])))
            {
                fileNames = (string[])e.Data.GetData(typeof(string[]));
            }
            else
            {
                fileNames = (String[])e.Data.GetData(DataFormats.FileDrop, true);
            }
            AddFiles(fileNames);
        }

        private void grid_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy; //you must add this line!!!!!!!
        }

        private void buttonDrag_MouseDown(object sender, MouseEventArgs e)
        {
            List<string> paths = new List<string>();
            foreach (DataGridViewRow row in grid_list.SelectedRows)
            {
                string path = row.Cells["path"].Value.ToString();
                paths.Add(path);
                //grid_list.Rows.Remove(row);
                //father.DeleteItem(indexNum, path);
            }
            if (buttonDrag.DoDragDrop(paths.ToArray<string>(), DragDropEffects.Copy) == DragDropEffects.Copy)
            {
                foreach (DataGridViewRow row in grid_list.SelectedRows)
                {
                    string path = row.Cells["path"].Value.ToString();
                    grid_list.Rows.Remove(row);
                    father.DeleteItem(indexNum, path);
                }
            }
        }

        private void form_Closing(object sender, FormClosingEventArgs e)
        {
            this.Hide();//窗口隐藏
            e.Cancel = true;//取消关闭
        }
    }
}
